<template>
  <el-dialog
    custom-class="vb-approve-dialog"
    title="发起审核"
    :visible="visible"
    :before-close="beforClose"
    append-to-body
    :wrapper-closable="false"
    :close-on-click-modal="false"
  >
    <div v-loading="loading" class="vb-approve-body">
      <ApproveForm v-if="modelId" :data-id="data.id" :company-id="companyId" :model-id="modelId" :staff-name="USER.nickName" :data="formData" @close="handleClose" @load="handleLoad" />
    </div>
  </el-dialog>
</template>
<script>
import ApproveForm from './form.vue'
import { getApproveModelId, addApproveSuccess } from '@/api/approve'
import { getOldCompanyId, getOldToken } from '@/utils/auth'
import { FormTem } from '@/components/Approve/enum'
import { getFormData } from '@/utils/approve'
export default {
  components: {
    ApproveForm
  },
  props: {
    data: {
      type: Object,
      default: () => {
        return {}
      }
    },
    visible: {
      type: Boolean,
      default: false
    },
    type: {
      type: Number,
      default: 0
    }

  },
  data() {
    return {
      loading: false,
      companyId: getOldCompanyId(),
      formData: {},
      modelId: ''
    }
  },
  async mounted() {
    const type = {
      5: 'quoteFormTem',
      6: 'contractFormTem',
      10: 'orderFormTem',
      9: 'payFormTem',
      11: 'invoiveFormTem'
    }
    this.loading = true
    const formTem = FormTem[type[this.type]]
    this.formData = getFormData(this.data, formTem)
    console.log('formData:', this.formData)
    const data = await getApproveModelId({
      'companyId': this.companyId,
      'oldToken': getOldToken(),
      'type': this.type // 类型5-报价、6-合同、9-回款、10-订单、11-开票
    }).catch(ex => {
      this.loading = false
      this.$message.error(ex.message || '获取流程模板失败')
    })
    this.modelId = data.modelId
  },
  methods: {
    handleLoad() {
      this.loading = false
    },
    beforClose() {
      this.$emit('close')
    },
    handleClose(v) {
      console.log('addApproveSuccess:', v)
      if (v) {
        addApproveSuccess({
          'callbackId': v.data,
          'companyId': getOldCompanyId(),
          'extId': this.data.id,
          'type': this.type
        }).then(res => {
          console.log('addApproveSuccess----addApproveSuccess')
          this.$emit('close', true)
        })
      } else {
        this.$emit('close')
      }
    }
  }
}
</script>
